import { Modal } from "ant-design-vue";

/**
 * 操作确认
 */
export function confirm({ title = "提示", content = "是否确认操作？" } = {}) {
  return new Promise((resolve, reject) => {
    Modal.confirm({
      title: title,
      content: content,
      centered: true,
      okText: "确定",
      cancelText: "取消",
      onOk() {
        resolve(true);
      },
      onCancel() {
        reject(false);
      },
    });
  });
}

/**
 * 导出文件数据
 */
export function exportFileData(
  res,
  exportFileName,
  type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8"
) {
  const fileName =
    exportFileName ||
    decodeURI(res.headers["content-disposition"].split("=")[1]);
  const blob = new Blob([res.data], { type: type });
  if (window.navigator.msSaveOrOpenBlob) {
    navigator.msSaveBlob(blob, fileName);
  } else {
    const downloadElement = document.createElement("a");
    const href = window.URL.createObjectURL(blob); // 创建下载的链接
    downloadElement.href = href;
    downloadElement.download = fileName; // 下载后文件名
    console.log("downloadElement", href, fileName);
    document.body.appendChild(downloadElement);
    downloadElement.click(); // 点击下载
    document.body.removeChild(downloadElement); // 下载完成移除元素
    window.URL.revokeObjectURL(href); // 释放掉blob对象
  }
}